package demo4;

import java.util.*;

public class Main {

    //
    public static void main1(String[] args){
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] nums = new int[n + 1];
        for(int i = 1; i <= n; i++){
            nums[i] = in.nextInt();
        }

        int[] f = new int[n + 1];
        int[] g = new int[n + 1];
        f[1] = nums[1];
        for(int i = 2; i <= n; i++){
            int x = nums[i];
            f[i] = Math.max(f[i - 1], x);
            if(x >= f[i - 1]){
                g[i] = f[i - 1];
            }else if(x >= g[i - 1]){
                g[i] = x;
            }else{
                g[i] = g[i - 1];
            }

        }

        int q = in.nextInt();
        while(q-- != 0){
            int x = in.nextInt();
            System.out.println(g[x]);
        }
    }

    //
    static int n;
    static int[][] nums;

    public static void setRow(){
        for(int i = 0; i < n / 2; i++){
            for(int j = 0; j < n; j++){
                int tmp = nums[i][j];
                nums[i][j] = nums[n - i - 1][j];
                nums[n - i - 1][j] = tmp;
            }
        }
    }
    public static void setCol(){
        for(int j = 0; j < n / 2; j++){
            for(int i = 0; i < n; i++){
                int tmp = nums[i][j];
                nums[i][j] = nums[i][n - j - 1];
                nums[i][n - j - 1] = tmp;
            }
        }
    }

    public static void main2(String[] args){
        Scanner in = new Scanner(System.in);
        n = in.nextInt();
        nums = new int[n][n];
        for(int i = 0; i < n; i++){
            for(int j = 0; j < n; j++){
                nums[i][j] = in.nextInt();
            }
        }

        int q = in.nextInt();
        int row = 0, col = 0;
        while(q-- != 0){
            int x = in.nextInt();
            if(x == 1){
                row++;
                col++;
            }else{
                row++;
            }
        }
        row %= 2; col %= 2;
        if(row != 0) { setRow();}
        if(col != 0) { setCol();}

        for(int i = 0; i < n; i++){
            for(int j = 0; j < n; j++){
                System.out.print(nums[i][j] + " ");
            }
            System.out.println("");
        }
    }

    //
    public static void main3(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt(), k = in.nextInt();
        long[] nums = new long[n + 1];
        for(int i = 1; i <= n; i++){
            nums[i] = in.nextLong();
        }

        long[][] dp = new long[n + 1][k];
        for(int i = 0; i <= n; i++){
            for(int j = 0; j < k; j++){
                dp[i][j] = (long) -1e16;
            }
        }
        dp[0][0] = 0;

        for(int i = 1; i <= n; i++){
            for(int j = 0; j < k; j++){
                dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][(int)((j - nums[i] % (long)k + (long)k) % (long)k)] + nums[i]);
            }
        }

        if(dp[n][0] > 0) {
            System.out.println(dp[n][0]);
        }else{
            System.out.println(-1);
        }
    }
}
